package cn.bcsp.hotevent.controller;


import cn.bcsp.hotevent.pojo.Hotevents;
import cn.bcsp.hotevent.service.IHoteventsService;
import cn.bcsp.hotevent.util.PageUtil;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-08-21
 */
@Controller
@RequestMapping("/hotevents")
@Api(description = "热文信息处理")
public class HoteventsController {

    @Resource
    private IHoteventsService iHoteventsService;

    @PostMapping(value = "/show", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "展示热文信息", produces = "application/json;charset=utf-8", httpMethod = "GET")
    @ResponseBody
    public PageUtil<Hotevents> show(@RequestParam(value = "currentPage", required = false) Integer currentPage,
                                    @RequestParam(value = "keyWord", required = false) String keyWord) {
        return iHoteventsService.querySysUserByPage(keyWord, currentPage);
    }


    @GetMapping("/del/{id}")
    @ApiOperation("删除信息")
    @ResponseBody
    public Object delete(@PathVariable("id") Integer id) {
        boolean res = iHoteventsService.removeById(id);
        return JSON.toJSONString("res");
    }

    @RequestMapping("/index")
    public String shows() {
        return "hotevents";
    }

    @PostMapping("/findId/{id}")
    @ApiOperation("删除信息")
    @ResponseBody
    public Object findById(@PathVariable("id") Integer id) {
        Hotevents hotevents = iHoteventsService.queryById(id);
        Map<String, Object> map = new HashMap<>();

        if (hotevents == null) {
            map.put("result", null);
        }

        map.put("result", hotevents);
        return JSON.toJSONString(map);
    }


    @GetMapping("/toDetail/{id}")
    @ApiOperation("详情页面")
    public String toDetail(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("id", id);
        return "hoteventsdetail";
    }

}
